#pragma once

#include "loirey_Geometry.h"

// [*] CGeometry::CPolygon

// -1: in
// +0: on
// +1: out
int CGeometry::CPolygon::InOnOut(const CPoint_2D_Double& p, const CSimpleTypeArray<CPoint_2D_Double>& P)
{
	int i;
	int count;
	double tx;
	const CPoint_2D_Double* qa;
	const CPoint_2D_Double* qb;
	const CPoint_2D_Double* qt;

	count = 0;
	for ( i = 0; i < (int)P.Size; ++i )
	{
		qa = P.pElement + i;
		qb = (i==(int)P.Size-1 ? P.pElement : qa+1);
		if (!(qa->y<qb->y)) { qt = qa; qa = qb; qb = qt; }
		if (!(qa->y+EPS<qb->y) && (p.y>qa->y-EPS) && (p.y<qb->y+EPS))
		{
			if (p.x>qa->x-EPS && p.x<qb->x+EPS) return 0;
			if (p.x>qb->x-EPS && p.x<qa->x+EPS) return 0;
			continue;
		}
		if (!(qa->y<p.y) || (qb->y<p.y)) continue;
		tx = qa->x + (qb->x - qa->x) * (p.y - qa->y) / (qb->y - qa->y);
		if (p.x > tx+EPS) continue;
		if (p.x < tx-EPS)
			++count;
		else
			return 0;
	}
	if (count & 1)
		return -1;
	else
		return +1;
}

